
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/5.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFLaunchScreen: UIView {
    @objc public var videoAreaY:CGFloat = 0
    @objc public var videoAreaX:CGFloat = 0
    @objc public let onelayerView = UIImageView()
    @objc public let arrow = UIImageView()
    @objc public let video = UIImageView()
    @objc public let graffiti = UIImageView()
    @objc public let background = UIView()
    @objc public let net = UIButton()//下一步
    @objc public let net2 = UIButton()//下一步
    @objc public let arrow2 = UIImageView()
    @objc public let twolayerView = UIImageView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        let islaunchScreen = self.islaunchScreen()
        
        if(islaunchScreen=="0")
        {
            
            self.backgroundColor = UIColor(red: 0.06, green: 0.06, blue: 0.07, alpha: 0.7)
     
            self.background.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
      
            self.addSubview(self.onelayerView)
            self.onelayerView.image = UIImage.init(named: "HTLive.bundle/shuoming")
            
       
            self.addSubview(self.arrow)
           
            if (UIDevice.current.userInterfaceIdiom == .phone) {
                self.arrow.image = UIImage.init(named: "HTLive.bundle/downacc")
            }else{
                self.arrow.image = UIImage.init(named: "HTLive.bundle/accIcon")
            }
  
            self.addSubview(self.video)
            
            self.video.image = UIImage.init(named: "HTLive.bundle/camera")
            self.addSubview(self.net)
            self.net.setImage(UIImage.init(named: "HTLive.bundle/next1"), for: .normal)
            self.net.addTarget(self, action: #selector(self.netClick), for: .touchUpInside)
           
            
            //第二页图片
            self.addSubview(self.graffiti)
            self.graffiti.image = UIImage.init(named: "HTLive.bundle/tuya")
   
            self.addSubview(self.arrow2)
            self.arrow2.image = UIImage.init(named: "HTLive.bundle/accIcon2")
            

            self.addSubview(self.twolayerView)
            self.twolayerView.image = UIImage.init(named: "HTLive.bundle/shuoming2")
            
            self.addSubview(self.net2)
    
            self.net2.setImage(UIImage.init(named: "HTLive.bundle/next2"), for: .normal)
            self.net2.addTarget(self, action: #selector(self.netClick2), for: .touchUpInside)
           
            self.graffiti.isHidden = true
            self.arrow2.isHidden = true
            self.twolayerView.isHidden = true
            self.net2.isHidden = true
        }
        else{
            self.isHidden = true
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.background.frame = self.bounds
        
        
        let videoY = self.videoAreaY - 64.0
        
        
        self.video.frame = CGRect(x: self.videoAreaX, y: videoY, width: 122.0, height: 64.0)
   
        
        var arrowY = self.videoAreaY;
        var arrowX = self.videoAreaX  - 63.0 - 10.0
        
        if (UIDevice.current.userInterfaceIdiom == .phone) {
            arrowY = arrowY - 50.0 - 57.0
            arrowX = arrowX - 22.0
        }
        
        
        self.arrow.frame = CGRect(x: arrowX, y: arrowY, width: 63.0, height: 57.0)
        
        
        var onelayerViewX = arrowX - 239.0 - 34.0
        var onelayerViewY = arrowY + 22.0
        
        if (UIDevice.current.userInterfaceIdiom == .phone)
        {
            onelayerViewY = arrowY - 63.0
        }
            self.onelayerView.frame = CGRect(x: onelayerViewX, y: onelayerViewY, width: 239.0, height: 63.0)
         
        
            let netX = onelayerViewX + (239.0/2.0) - (153.0/2.0);
            let netY = onelayerViewY + 63.0 + 60.0;
        
            self.net.frame = CGRect(x: netX, y: netY, width: 153.0, height: 51.0)
        
        
        
            var graffitiY = self.frame.size.height - 312.0 - 5.0;
            if (UIDevice.current.userInterfaceIdiom == .phone)
              {
        
            
            graffitiY = self.frame.size.height - 217.0 - 5.0;
            self.graffiti.frame = CGRect(x: 10.0, y: graffitiY, width: 230.0, height: 217.0)
              
            
        }else{
            
            self.graffiti.frame =  CGRect(x: 10.0, y: graffitiY, width: 332.0, height: 312.0)

        }
        
        
        
                var arrow2X = 10.0 + self.graffiti.frame.size.width + 20;
        
                var arrow2Y = self.frame.size.height - 211.0;
        if (UIDevice.current.userInterfaceIdiom == .phone) {
            
        
            arrow2X = 10.0 + self.graffiti.frame.size.width + 10;
            arrow2Y = self.frame.size.height - 197.0 - 60.0;
            //5S
            if (self.frame.size.width < 569.0){
                //箭头2
                self.arrow2.frame = CGRect(x: arrow2X, y: arrow2Y, width: 90.0, height: 56.5)
              
            }else{
                //箭头2
                self.arrow2.frame =  CGRect(x: arrow2X, y: arrow2Y, width: 136.0, height: 90.0)
              
            }
            
            
        }else{
            //箭头2
            self.arrow2.frame = CGRect(x: arrow2X, y: arrow2Y, width: 136.0, height: 90.0)
       
        }
        
        
        var twolayerViewX = arrow2X + 136.0;
        
      if (UIDevice.current.userInterfaceIdiom == .phone)
        {
            graffitiY = (self.graffiti.frame.origin.y) - 60.0;
       
            twolayerViewX = self.arrow2.frame.origin.x+self.arrow2.frame.size.width - 20.0;
  
            //说明 2
            self.twolayerView.frame = CGRect(x: twolayerViewX, y: graffitiY, width: 238.0, height: 103.0)

        }else{
            //说明 2
            self.twolayerView.frame =  CGRect(x: twolayerViewX, y: graffitiY, width: 238.0, height: 103.0)
   
        }

        //一次确认
            let net2Y = (self.twolayerView.frame.origin.y) +   (self.twolayerView.frame.size.height) + 30.0;
        
        let net2X = twolayerViewX + (238.0/2.0) - (153.0/2.0);
        
            self.net2.frame = CGRect(x: net2X, y: net2Y, width: 153.0, height: 51.0)
   
    }
    @objc func netClick()
    {
        self.onelayerView.isHidden = true
        self.arrow.isHidden = true
        self.video.isHidden = true
        self.net.isHidden = true
        
        
        self.graffiti.isHidden = false
        self.arrow2.isHidden = false
        self.twolayerView.isHidden = false
        self.net2.isHidden = false
       
        let userDefault = UserDefaults.standard
        userDefault.set("1", forKey: "islaunchScreen")
        userDefault.synchronize()
    }

    @objc func netClick2()
    {
        self.onelayerView.isHidden = true
        self.arrow.isHidden = true
        self.video.isHidden = true
        self.net.isHidden = true

        self.graffiti.isHidden = true
        self.arrow2.isHidden = true
        self.twolayerView.isHidden = true
        self.net2.isHidden = true
        self.background.isHidden = true
        self.isHidden = true
    }
    //是否已经启动过
    func islaunchScreen()->String
    {
        
        
        let userDefault = UserDefaults.standard
        let islaunchScreen = userDefault.object(forKey: "islaunchScreen")
      
      
        if (islaunchScreen != nil){
            
            if(islaunchScreen is String){
                
                if (islaunchScreen as! String == "1") {
                    
                     return "1"
                }
              
               
                }
                
        
            
        }else{
            
            userDefault.set("0", forKey: "islaunchScreen")
          
    
        }
        
        userDefault.synchronize()
        
        return "0"
    }
    
    

}
